home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / minix / libsrc~1.z / libsrc~1 / gnulib2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-28  |  18.5 KB  |  968 lines

  1. /* WARNING: compile this in 32 bit int mode even for short library */
  2.  
  3. #ifdef __DEF_ALL__    /* this def'ed when making on the ST */
  4.  
  5. #define L_adddi3
  6. #define L_subdi3
  7. #define L_muldi3
  8. #define L_divdi3
  9. #define L_moddi3
  10. #define L_udivdi3
  11. #define L_umoddi3
  12. #define L_negdi2
  13. #define L_anddi3
  14. #define L_iordi3
  15. #define L_xordi3
  16. #define L_lshrdi3
  17. #define L_lshldi3
  18. #define L_ashldi3
  19. #define L_ashrdi3
  20. #define L_one_cmpldi2
  21. #define L_bdiv
  22. #define L_cmpdi2
  23. #define L_ucmpdi2
  24. #define L_fixunsdfdi
  25. #define L_fixdfdi
  26. #define L_floatdidf
  27.  
  28. #endif /* __DEF_ALL__ */
  29.  
  30. /* More subroutines needed by GCC output code on some machines.  */
  31. /* Compile this one with gcc.  */
  32.  
  33. #if 0
  34. #include "config.h"    /* dont drag this in, just define relevant
  35.                stuff from xm/tm-atari.h & xm/tm-m68k.h here */
  36. #else
  37.  
  38. /* #defines that need visibility everywhere.  */
  39. #define FALSE 0
  40. #define TRUE 1
  41.  
  42. /* This describes the machine the compiler is hosted on.  */
  43. #define HOST_BITS_PER_CHAR 8
  44. #define HOST_BITS_PER_SHORT 16
  45. #define HOST_BITS_PER_INT 32
  46. #define HOST_BITS_PER_LONG 32
  47.  
  48. #ifdef __GNUC__
  49. #ifndef minix
  50. #define alloca __builtin_alloca
  51. #else
  52.      void *alloca(unsigned long);
  53. #endif
  54. #endif
  55.  
  56. /* Define this if most significant bit is lowest numbered
  57.    in instructions that operate on numbered bit-fields.
  58.    This is true for 68020 insns such as bfins and bfexts.
  59.    We make it true always by avoiding using the single-bit insns
  60.    except in special cases with constant bit numbers.  */
  61. #define BITS_BIG_ENDIAN
  62.  
  63. /* Define this if most significant byte of a word is the lowest numbered.  */
  64. /* That is true on the 68000.  */
  65. #define BYTES_BIG_ENDIAN
  66.  
  67. /* Define this if most significant word of a multiword number is numbered.  */
  68. /* For 68000 we can decide arbitrarily
  69.    since there are no machine instructions for them.  */
  70. /* #define WORDS_BIG_ENDIAN */
  71.  
  72. /* number of bits in an addressible storage unit */
  73. #define BITS_PER_UNIT 8
  74.  
  75. /* Width in bits of a "word", which is the contents of a machine register.
  76.    Note that this is not necessarily the width of data type `int';
  77.    if using 16-bit ints on a 68000, this would still be 32.
  78.    But on a machine with 16-bit registers, this would be 16.  */
  79. #define BITS_PER_WORD 32
  80.  
  81. /* Width of a word, in units (bytes).  */
  82. #define UNITS_PER_WORD 4
  83.  
  84. /* Width in bits of a pointer.
  85.    See also the macro `Pmode' defined below.  */
  86. #define POINTER_SIZE 32
  87.  
  88. /* Allocation boundary (in *bits*) for storing pointers in memory.  */
  89. #define POINTER_BOUNDARY 16
  90.  
  91. /* Allocation boundary (in *bits*) for storing arguments in argument list.  */
  92. #define PARM_BOUNDARY (TARGET_SHORT ? 16 : 32)
  93.  
  94. /* Boundary (in *bits*) on which stack pointer should be aligned.  */
  95. #define STACK_BOUNDARY 16
  96.  
  97. /* Allocation boundary (in *bits*) for the code of a function.  */
  98. #define FUNCTION_BOUNDARY 16
  99.  
  100. /* Alignment of field after `int : 0' in a structure.  */
  101. #define EMPTY_FIELD_BOUNDARY 16
  102.  
  103. /* No data type wants to be aligned rounder than this.  */
  104. #define BIGGEST_ALIGNMENT 16
  105.  
  106. /* Define this if move instructions will actually fail to work
  107.    when given unaligned data.  */
  108. #define STRICT_ALIGNMENT
  109.  
  110. /* Define number of bits in most basic integer type.
  111.    (If undefined, default is BITS_PER_WORD).  */
  112. #ifdef __MSHORT__
  113. #define INT_TYPE_SIZE 16
  114. #else
  115. #define INT_TYPE_SIZE 32
  116. #endif
  117.  
  118. #endif
  119.  
  120. #ifndef minix
  121. #include <stddef.h>
  122. #else
  123.      typedef unsigned long Size_T;
  124. #include "lib.h"
  125. #endif
  126.  
  127. #ifndef SItype
  128. #define SItype long int
  129. #endif
  130.  
  131. /* long long ints are pairs of long ints in the order determined by
  132.    WORDS_BIG_ENDIAN.  */
  133.  
  134. #ifdef WORDS_BIG_ENDIAN
  135.   struct longlong {long high, low;};
  136. #else
  137.   struct longlong {long low, high;};
  138. #endif
  139.  
  140. /* We need this union to unpack/pack longlongs, since we don't have
  141.    any arithmetic yet.  Incoming long long parameters are stored
  142.    into the `ll' field, and the unpacked result is read from the struct
  143.    longlong.  */
  144.  
  145. typedef union
  146. {
  147.   struct longlong s;
  148.   long long ll;
  149.   SItype i[2];
  150.   unsigned SItype ui[2];
  151. } long_long;
  152.  
  153. /* Internally, long long ints are strings of unsigned shorts in the
  154.    order determined by BYTES_BIG_ENDIAN.  */
  155.  
  156. #define B 0x10000
  157. #define low16 (B - 1)
  158.  
  159. #ifdef BYTES_BIG_ENDIAN
  160.  
  161. /* Note that HIGH and LOW do not describe the order
  162.    of words in a long long.  They describe the order of words
  163.    in vectors ordered according to the byte order.  */
  164.  
  165. #define HIGH 0
  166. #define LOW 1
  167.  
  168. #define big_end(n)    0 
  169. #define little_end(n)    ((n) - 1)
  170. #define next_msd(i)    ((i) - 1)
  171. #define next_lsd(i)    ((i) + 1)
  172. #define is_not_msd(i,n)    ((i) >= 0)
  173. #define is_not_lsd(i,n)    ((i) < (n))
  174.  
  175. #else
  176.  
  177. #define LOW 0
  178. #define HIGH 1
  179.  
  180. #define big_end(n)    ((n) - 1)
  181. #define little_end(n)    0 
  182. #define next_msd(i)    ((i) + 1)
  183. #define next_lsd(i)    ((i) - 1)
  184. #define is_not_msd(i,n)    ((i) < (n))
  185. #define is_not_lsd(i,n)    ((i) >= 0)
  186.  
  187. #endif
  188.  
  189. /* These algorithms are all straight out of Knuth, vol. 2, sec. 4.3.1. */
  190.  
  191. static int badd ();
  192. static int bsub ();
  193. static void bmul ();
  194. static int bneg ();
  195. static int bshift ();
  196.  
  197. #ifdef L_adddi3
  198. long long 
  199. __adddi3 (u, v)
  200.      long long u, v;
  201. {
  202.   long a[2], b[2], c[2];
  203.   long_long w;
  204.   long_long uu, vv;
  205.  
  206.   uu.ll = u;
  207.   vv.ll = v;
  208.  
  209.   a[HIGH] = uu.s.high;
  210.   a[LOW] = uu.s.low;
  211.   b[HIGH] = vv.s.high;
  212.   b[LOW] = vv.s.low;
  213.  
  214.   badd (a, b, c, sizeof c);
  215.  
  216.   w.s.high = c[HIGH];
  217.   w.s.low = c[LOW];
  218.   return w.ll;
  219. }
  220.  
  221. static int 
  222. badd (a, b, c, n)
  223.      unsigned short *a, *b, *c;
  224.      Size_T n;
  225. {
  226.   unsigned long acc;
  227.   int i;
  228.  
  229.   n /= sizeof *c;
  230.  
  231.   acc = 0;
  232.   for (i = little_end (n); is_not_msd (i, n); i = next_msd (i))
  233.     {
  234.       /* Widen before adding to avoid loss of high bits.  */
  235.       acc += (unsigned long) a[i] + b[i];
  236.       c[i] = acc & low16;
  237.       acc = acc >> 16;
  238.     }
  239.   return acc;
  240. }
  241. #endif
  242.  
  243. #ifdef L_anddi3
  244. long long 
  245. __anddi3 (u, v)
  246.      long long u, v;
  247. {
  248.   long_long w;
  249.   long_long uu, vv;
  250.  
  251.   uu.ll = u;
  252.   vv.ll = v;
  253.  
  254.   w.s.high = uu.s.high & vv.s.high;
  255.   w.s.low = uu.s.low & vv.s.low;
  256.  
  257.   return w.ll;
  258. }
  259. #endif
  260.  
  261. #ifdef L_iordi3
  262. long long 
  263. __iordi3 (u, v)
  264.      long long u, v;
  265. {
  266.   long_long w;
  267.   long_long uu, vv;
  268.  
  269.   uu.ll = u;
  270.   vv.ll = v;
  271.  
  272.   w.s.high = uu.s.high | vv.s.high;
  273.   w.s.low = uu.s.low | vv.s.low;
  274.  
  275.   return w.ll;
  276. }
  277. #endif
  278.  
  279. #ifdef L_xordi3
  280. long long 
  281. __xordi3 (u, v)
  282.      long long u, v;
  283. {
  284.   long_long w;
  285.   long_long uu, vv;
  286.  
  287.   uu.ll = u;
  288.   vv.ll = v;
  289.  
  290.   w.s.high = uu.s.high ^ vv.s.high;
  291.   w.s.low = uu.s.low ^ vv.s.low;
  292.  
  293.   return w.ll;
  294. }
  295. #endif
  296.  
  297. #ifdef L_one_cmpldi2
  298. long long
  299. __one_cmpldi2 (u)
  300.      long long u;
  301. {
  302.   long_long w;
  303.   long_long uu;
  304.  
  305.   uu.ll = u;
  306.  
  307.   w.s.high = ~uu.s.high;
  308.   w.s.low = ~uu.s.low;
  309.  
  310.   return w.ll;
  311. }
  312. #endif
  313.  
  314. #ifdef L_lshldi3
  315. long long
  316. __lshldi3 (u, b)
  317.      long long u;
  318.      long int b;
  319. {
  320.   long_long w;
  321.   unsigned long carries;
  322.   int bm;
  323.   long_long uu;
  324.  
  325.   if (b == 0)
  326.     return u;
  327.  
  328.   uu.ll = u;
  329.  
  330.   bm = (sizeof (int) * BITS_PER_UNIT) - b;
  331.   if (bm <= 0)
  332.     {
  333.       w.s.low = 0;
  334.       w.s.high = (unsigned long)uu.s.low << -bm;
  335.     }
  336.   else
  337.     {
  338.       carries = (unsigned long)uu.s.low >> bm;
  339.       w.s.low = (unsigned long)uu.s.low << b;
  340.       w.s.high = ((unsigned long)uu.s.high << b) | carries;
  341.     }
  342.  
  343.   return w.ll;
  344. }
  345. #endif
  346.  
  347. #ifdef L_lshrdi3
  348. long long
  349. __lshrdi3 (u, b)
  350.      long long u;
  351.      long int b;
  352. {
  353.   long_long w;
  354.   unsigned long carries;
  355.   int bm;
  356.   long_long uu;
  357.  
  358.   if (b == 0)
  359.     return u;
  360.  
  361.   uu.ll = u;
  362.  
  363.   bm = (sizeof (int) * BITS_PER_UNIT) - b;
  364.   if (bm <= 0)
  365.     {
  366.       w.s.high = 0;
  367.       w.s.low = (unsigned long)uu.s.high >> -bm;
  368.     }
  369.   else
  370.     {
  371.       carries = (unsigned long)uu.s.high << bm;
  372.       w.s.high = (unsigned long)uu.s.high >> b;
  373.       w.s.low = ((unsigned long)uu.s.low >> b) | carries;
  374.     }
  375.  
  376.   return w.ll;
  377. }
  378. #endif
  379.  
  380. #ifdef L_ashldi3
  381. long long
  382. __ashldi3 (u, b)
  383.      long long u;
  384.      long int b;
  385. {
  386.   long_long w;
  387.   unsigned long carries;
  388.   int bm;
  389.   long_long uu;
  390.  
  391.   if (b == 0)
  392.     return u;
  393.  
  394.   uu.ll = u;
  395.  
  396.   bm = (sizeof (int) * BITS_PER_UNIT) - b;
  397.   if (bm <= 0)
  398.     {
  399.       w.s.low = 0;
  400.       w.s.high = (unsigned long)uu.s.low << -bm;
  401.     }
  402.   else
  403.     {
  404.       carries = (unsigned long)uu.s.low >> bm;
  405.       w.s.low = (unsigned long)uu.s.low << b;
  406.       w.s.high = ((unsigned long)uu.s.high << b) | carries;
  407.     }
  408.  
  409.   return w.ll;
  410. }
  411. #endif
  412.  
  413. #ifdef L_ashrdi3
  414. long long
  415. __ashrdi3 (u, b)
  416.      long long u;
  417.      long int b;
  418. {
  419.   long_long w;
  420.   unsigned long carries;
  421.   int bm;
  422.   long_long uu;
  423.  
  424.   if (b == 0)
  425.     return u;
  426.  
  427.   uu.ll = u;
  428.  
  429.   bm = (sizeof (int) * BITS_PER_UNIT) - b;
  430.   if (bm <= 0)
  431.     {
  432.       w.s.high = uu.s.high >> 31; /* just to make w.s.high 1..1 or 0..0 */
  433.       w.s.low = uu.s.high >> -bm;
  434.     }
  435.   else
  436.     {
  437.       carries = (unsigned long)uu.s.high << bm;
  438.       w.s.high = uu.s.high >> b;
  439.       w.s.low = ((unsigned long)uu.s.low >> b) | carries;
  440.     }
  441.  
  442.   return w.ll;
  443. }
  444. #endif
  445.  
  446. #ifdef L_subdi3
  447. long long 
  448. __subdi3 (u, v)
  449.      long long u, v;
  450. {
  451.   long a[2], b[2], c[2];
  452.   long_long w;
  453.   long_long uu, vv;
  454.  
  455.   uu.ll = u;
  456.   vv.ll = v;
  457.  
  458.   a[HIGH] = uu.s.high;
  459.   a[LOW] = uu.s.low;
  460.   b[HIGH] = vv.s.high;
  461.   b[LOW] = vv.s.low;
  462.  
  463.   bsub (a, b, c, sizeof c);
  464.  
  465.   w.s.high = c[HIGH];
  466.   w.s.low = c[LOW];
  467.   return w.ll;
  468. }
  469.  
  470. static int 
  471. bsub (a, b, c, n)
  472.      unsigned short *a, *b, *c;
  473.      Size_T n;
  474. {
  475.   signed long acc;
  476.   int i;
  477.  
  478.   n /= sizeof *c;
  479.  
  480.   acc = 0;
  481.   for (i = little_end (n); is_not_msd (i, n); i = next_msd (i))
  482.     {
  483.       /* Widen before subtracting to avoid loss of high bits.  */
  484.       acc += (long) a[i] - b[i];
  485.       c[i] = acc & low16;
  486.       acc = acc >> 16;
  487.     }
  488.   return acc;
  489. }
  490. #endif
  491.  
  492. #ifdef L_muldi3
  493. long long 
  494. __muldi3 (u, v)
  495.      long long u, v;
  496. {
  497.   long a[2], b[2], c[2][2];
  498.   long_long w;
  499.   long_long uu, vv;
  500.  
  501.   uu.ll = u;
  502.   vv.ll = v;
  503.  
  504.   a[HIGH] = uu.s.high;
  505.   a[LOW] = uu.s.low;
  506.   b[HIGH] = vv.s.high;
  507.   b[LOW] = vv.s.low;
  508.  
  509.   bmul (a, b, c, sizeof a, sizeof b);
  510.  
  511.   w.s.high = c[LOW][HIGH];
  512.   w.s.low = c[LOW][LOW];
  513.   return w.ll;
  514. }
  515.  
  516. static void 
  517. bmul (a, b, c, m, n)
  518.     unsigned short *a, *b, *c;
  519.     Size_T m, n;
  520. {
  521.   int i, j;
  522.   unsigned long acc;
  523.  
  524.   bzero (c, m + n);
  525.  
  526.   m /= sizeof *a;
  527.   n /= sizeof *b;
  528.  
  529.   for (j = little_end (n); is_not_msd (j, n); j = next_msd (j))
  530.     {
  531.       unsigned short *c1 = c + j + little_end (2);
  532.       acc = 0;
  533.       for (i = little_end (m); is_not_msd (i, m); i = next_msd (i))
  534.     {
  535.       /* Widen before arithmetic to avoid loss of high bits.  */
  536.       acc += (unsigned long) a[i] * b[j] + c1[i];
  537.       c1[i] = acc & low16;
  538.       acc = acc >> 16;
  539.     }
  540.       c1[i] = acc;
  541.     }
  542. }
  543. #endif
  544.  
  545. #ifdef L_divdi3
  546. long long
  547. __divdi3 (u, v)
  548.      long long u, v;
  549. {
  550.   if (u < 0)
  551.     if (v < 0)
  552.       return (unsigned long long) -u / (unsigned long long) -v;
  553.     else
  554.       return - ((unsigned long long) -u / (unsigned long long) v);
  555.   else
  556.     if (v < 0)
  557.       return - ((unsigned long long) u / (unsigned long long) -v);
  558.     else
  559.       return (unsigned long long) u / (unsigned long long) v;
  560. }
  561. #endif
  562.  
  563. #ifdef L_moddi3
  564. long long
  565. __moddi3 (u, v)
  566.      long long u, v;
  567. {
  568.   if (u < 0)
  569.     if (v < 0)
  570.       return - ((unsigned long long) -u % (unsigned long long) -v);
  571.     else
  572.       return - ((unsigned long long) -u % (unsigned long long) v);
  573.   else
  574.     if (v < 0)
  575.       return (unsigned long long) u % (unsigned long long) -v;
  576.     else
  577.       return (unsigned long long) u % (unsigned long long) v;
  578. }
  579. #endif
  580.  
  581. #ifdef L_udivdi3
  582. long long 
  583. __udivdi3 (u, v)
  584.      long long u, v;
  585. {
  586.   unsigned long a[2][2], b[2], q[2], r[2];
  587.   long_long w;
  588.   long_long uu, vv;
  589.  
  590.   uu.ll = u;
  591.   vv.ll = v;
  592.  
  593.   a[HIGH][HIGH] = 0;
  594.   a[HIGH][LOW] = 0;
  595.   a[LOW][HIGH] = uu.s.high;
  596.   a[LOW][LOW] = uu.s.low;
  597.   b[HIGH] = vv.s.high;
  598.   b[LOW] = vv.s.low;
  599.  
  600.   __bdiv (a, b, q, r, sizeof a, sizeof b);
  601.  
  602.   w.s.high = q[HIGH];
  603.   w.s.low = q[LOW];
  604.   return w.ll;
  605. }
  606. #endif
  607.  
  608. #ifdef L_umoddi3
  609. long long 
  610. __umoddi3 (u, v)
  611.      long long u, v;
  612. {
  613.   unsigned long a[2][2], b[2], q[2], r[2];
  614.   long_long w;
  615.   long_long uu, vv;
  616.  
  617.   uu.ll = u;
  618.   vv.ll = v;
  619.  
  620.   a[HIGH][HIGH] = 0;
  621.   a[HIGH][LOW] = 0;
  622.   a[LOW][HIGH] = uu.s.high;
  623.   a[LOW][LOW] = uu.s.low;
  624.   b[HIGH] = vv.s.high;
  625.   b[LOW] = vv.s.low;
  626.  
  627.   __bdiv (a, b, q, r, sizeof a, sizeof b);
  628.  
  629.   w.s.high = r[HIGH];
  630.   w.s.low = r[LOW];
  631.   return w.ll;
  632. }
  633. #endif
  634.  
  635. #ifdef L_negdi2
  636. long long 
  637. __negdi2 (u)
  638.      long long u;
  639. {
  640.   unsigned long a[2], b[2];
  641.   long_long w;
  642.   long_long uu;
  643.  
  644.   uu.ll = u;
  645.  
  646.   a[HIGH] = uu.s.high;
  647.   a[LOW] = uu.s.low;
  648.  
  649.   bneg (a, b, sizeof b);
  650.  
  651.   w.s.high = b[HIGH];
  652.   w.s.low = b[LOW];
  653.   return w.ll;
  654. }
  655.  
  656. static int
  657. bneg (a, b, n)
  658.      unsigned short *a, *b;
  659.      Size_T n;
  660. {
  661.   signed long acc;
  662.   int i;
  663.  
  664.   n /= sizeof (short);
  665.  
  666.   acc = 0;
  667.   for (i = little_end (n); is_not_msd (i, n); i = next_msd (i))
  668.     {
  669.       acc -= a[i];
  670.       b[i] = acc & low16;
  671.       acc = acc >> 16;
  672.     }
  673.   return acc;
  674. }
  675. #endif
  676.  
  677. /* Divide a by b, producing quotient q and remainder r.
  678.  
  679.        sizeof a is m
  680.        sizeof b is n
  681.        sizeof q is m - n
  682.        sizeof r is n
  683.  
  684.    The quotient must fit in m - n bytes, i.e., the most significant
  685.    n digits of a must be less than b, and m must be greater than n.  */
  686.  
  687. /* The name of this used to be __div_internal,
  688.    but that is too long for SYSV.  */
  689.  
  690. #ifdef L_bdiv
  691. void 
  692. __bdiv (a, b, q, r, m, n)
  693.      unsigned short *a, *b, *q, *r;
  694.      Size_T m, n;
  695. {
  696.   unsigned long qhat, rhat;
  697.   unsigned long acc;
  698.   unsigned short *u = (unsigned short *) alloca (m);
  699.   unsigned short *v = (unsigned short *) alloca (n);
  700.   unsigned short *u0, *u1, *u2;
  701.   unsigned short *v0;
  702.   int d, qn;
  703.   int i, j;
  704.  
  705.   m /= sizeof *a;
  706.   n /= sizeof *b;
  707.   qn = m - n;
  708.  
  709.   /* Remove leading zero digits from divisor, and the same number of
  710.      digits (which must be zero) from dividend.  */
  711.  
  712.   while (b[big_end (n)] == 0)
  713.     {
  714.       r[big_end (n)] = 0;
  715.  
  716.       a += little_end (2);
  717.       b += little_end (2);
  718.       r += little_end (2);
  719.       m--;
  720.       n--;
  721.  
  722.       /* Check for zero divisor.  */
  723.       if (n == 0)
  724.     abort ();
  725.     }
  726.       
  727.   /* If divisor is a single digit, do short division.  */
  728.  
  729.   if (n == 1)
  730.     {
  731.       acc = a[big_end (m)];
  732.       a += little_end (2);
  733.       for (j = big_end (qn); is_not_lsd (j, qn); j = next_lsd (j))
  734.     {
  735.       acc = (acc << 16) | a[j];
  736.       q[j] = acc / *b;
  737.       acc = acc % *b;
  738.     }
  739.       *r = acc;
  740.       return;
  741.     }
  742.  
  743.   /* No such luck, must do long division. Shift divisor and dividend
  744.      left until the high bit of the divisor is 1.  */
  745.  
  746.   for (d = 0; d < 16; d++)
  747.     if (b[big_end (n)] & (1 << (16 - 1 - d)))
  748.       break;
  749.  
  750.   bshift (a, d, u, 0, m);
  751.   bshift (b, d, v, 0, n);
  752.  
  753.   /* Get pointers to the high dividend and divisor digits.  */
  754.  
  755.   u0 = u + big_end (m) - big_end (qn);
  756.   u1 = next_lsd (u0);
  757.   u2 = next_lsd (u1);
  758.   u += little_end (2);
  759.  
  760.   v0 = v + big_end (n);
  761.  
  762.   /* Main loop: find a quotient digit, multiply it by the divisor,
  763.      and subtract that from the dividend, shifted over the right amount. */
  764.  
  765.   for (j = big_end (qn); is_not_lsd (j, qn); j = next_lsd (j))
  766.     {
  767.       /* Quotient digit initial guess: high 2 dividend digits over high
  768.      divisor digit.  */
  769.  
  770.       if (u0[j] == *v0)
  771.     {
  772.       qhat = B - 1;
  773.       rhat = (unsigned long) *v0 + u1[j];
  774.     }
  775.       else
  776.     {
  777.       unsigned long numerator = ((unsigned long) u0[j] << 16) | u1[j];
  778.       qhat = numerator / *v0;
  779.       rhat = numerator % *v0;
  780.     }
  781.  
  782.       /* Now get the quotient right for high 3 dividend digits over
  783.      high 2 divisor digits.  */
  784.  
  785.       while (rhat < B && qhat * *next_lsd (v0) > ((rhat << 16) | u2[j]))
  786.     {
  787.       qhat -= 1;
  788.       rhat += *v0;
  789.     }
  790.         
  791.       /* Multiply quotient by divisor, subtract from dividend.  */
  792.  
  793.       acc = 0;
  794.       for (i = little_end (n); is_not_msd (i, n); i = next_msd (i))
  795.     {
  796.       acc += (unsigned long) (u + j)[i] - v[i] * qhat;
  797.       (u + j)[i] = acc & low16;
  798.       if (acc < B)
  799.         acc = 0;
  800.       else
  801.         acc = (acc >> 16) | -B;
  802.     }
  803.  
  804.       q[j] = qhat;
  805.  
  806.       /* Quotient may have been too high by 1.  If dividend went negative,
  807.      decrement the quotient by 1 and add the divisor back.  */
  808.  
  809.       if ((signed long) (acc + u0[j]) < 0)
  810.     {
  811.       q[j] -= 1;
  812.       acc = 0;
  813.       for (i = little_end (n); is_not_msd (i, n); i = next_msd (i))
  814.         {
  815.           acc += (unsigned long) (u + j)[i] + v[i];
  816.           (u + j)[i] = acc & low16;
  817.           acc = acc >> 16;
  818.         }
  819.     }
  820.     }
  821.  
  822.   /* Now the remainder is what's left of the dividend, shifted right
  823.      by the amount of the normalizing left shift at the top.  */
  824.  
  825.   r[big_end (n)] = bshift (u + 1 + little_end (j - 1),
  826.                16 - d,
  827.                r + little_end (2),
  828.                u[little_end (m - 1)] >> d,
  829.                n - 1);
  830. }
  831.  
  832. /* Left shift U by K giving W; fill the introduced low-order bits with
  833.    CARRY_IN.  Length of U and W is N.  Return carry out.  K must be
  834.    in 0 .. 16.  */
  835.  
  836. static int
  837. bshift (u, k, w, carry_in, n)
  838.      unsigned short *u, *w, carry_in;
  839.      int k, n;
  840. {
  841.   unsigned long acc;
  842.   int i;
  843.  
  844.   if (k == 0)
  845.     {
  846.       bcopy (u, w, n * sizeof *u);
  847.       return 0;
  848.     }
  849.  
  850.   acc = carry_in;
  851.   for (i = little_end (n); is_not_msd (i, n); i = next_msd (i))
  852.     {
  853.       acc |= (unsigned long) u[i] << k;
  854.       w[i] = acc & low16;
  855.       acc = acc >> 16;
  856.     }
  857.   return acc;
  858. }
  859. #endif
  860.  
  861. #ifdef L_cmpdi2
  862. SItype
  863. __cmpdi2 (a, b)
  864.      long long a, b;
  865. {
  866.   long_long au, bu;
  867.  
  868.   au.ll = a, bu.ll = b;
  869.  
  870.   if (au.s.high < bu.s.high)
  871.     return 0;
  872.   else if (au.s.high > bu.s.high)
  873.     return 2;
  874.   if ((unsigned) au.s.low < (unsigned) bu.s.low)
  875.     return 0;
  876.   else if ((unsigned) au.s.low > (unsigned) bu.s.low)
  877.     return 2;
  878.   return 1;
  879. }
  880. #endif
  881.  
  882. #ifdef L_ucmpdi2
  883. SItype
  884. __ucmpdi2 (a, b)
  885.      long long a, b;
  886. {
  887.   long_long au, bu;
  888.  
  889.   au.ll = a, bu.ll = b;
  890.  
  891.   if ((unsigned) au.s.high < (unsigned) bu.s.high)
  892.     return 0;
  893.   else if ((unsigned) au.s.high > (unsigned) bu.s.high)
  894.     return 2;
  895.   if ((unsigned) au.s.low < (unsigned) bu.s.low)
  896.     return 0;
  897.   else if ((unsigned) au.s.low > (unsigned) bu.s.low)
  898.     return 2;
  899.   return 1;
  900. }
  901. #endif
  902.  
  903. #ifdef L_fixunsdfdi
  904. #define HIGH_WORD_COEFF (((long long) 1) << BITS_PER_WORD)
  905.  
  906. long long
  907. __fixunsdfdi (a)
  908.      double a;
  909. {
  910.   double b;
  911.   unsigned long long v;
  912.  
  913.   if (a < 0)
  914.     return 0;
  915.  
  916.   /* Compute high word of result, as a flonum.  */
  917.   b = (a / HIGH_WORD_COEFF);
  918.   /* Convert that to fixed (but not to long long!),
  919.      and shift it into the high word.  */
  920.   v = (unsigned long int) b;
  921.   v <<= BITS_PER_WORD;
  922.   /* Remove high part from the double, leaving the low part as flonum.  */
  923.   a -= (double)v;
  924.   /* Convert that to fixed (but not to long long!) and add it in.
  925.      Sometimes A comes out negative.  This is significant, since
  926.      A has more bits than a long int does.  */
  927.   if (a < 0)
  928.     v -= (unsigned long int) (- a);
  929.   else
  930.     v += (unsigned long int) a;
  931.   return v;
  932. }
  933. #endif
  934.  
  935. #ifdef L_fixdfdi
  936. long long
  937. __fixdfdi (a)
  938.      double a;
  939. {
  940.   if (a < 0)
  941.     return - __fixunsdfdi (-a);
  942.   return __fixunsdfdi (a);
  943. }
  944. #endif
  945.  
  946. #ifdef L_floatdidf
  947. #define HIGH_HALFWORD_COEFF (((long long) 1) << (BITS_PER_WORD / 2))
  948. #define HIGH_WORD_COEFF (((long long) 1) << BITS_PER_WORD)
  949.  
  950. double
  951. __floatdidf (u)
  952.      long long u;
  953. {
  954.   double d;
  955.   int negate = 0;
  956.  
  957.   if (u < 0)  /* was : if (d < 0)     */
  958.     u = -u, negate = 1;
  959.  
  960.   d = (unsigned int) (u >> BITS_PER_WORD);
  961.   d *= HIGH_HALFWORD_COEFF;
  962.   d *= HIGH_HALFWORD_COEFF;
  963.   d += (unsigned int) (u & (HIGH_WORD_COEFF - 1));
  964.  
  965.   return (negate ? -d : d);
  966. }
  967. #endif
  968.